package com.ty.xiaoyu.service.impl;

import com.ty.xiaoyu.entity.Notify;
import com.ty.xiaoyu.exception.NotifyOperationException;
import com.ty.xiaoyu.service.NotifyService;
import com.ty.xiaoyu.util.MyTimeUtil;
import com.ty.xiaoyu.util.RandomIdUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NotifyServiceImpl extends BaseService implements NotifyService {
    @Override
    public void addNotify(Notify notify) throws NotifyOperationException {
        notify.setNotifyId(RandomIdUtil.getId());
        notify.setCreateTime(MyTimeUtil.nowTime());
        notify.setIsRead(1);
        notify.setNotifyState(1);
        Integer state = notifyMapper.insertNotify(notify);
        if (state <= 0) {
            throw new NotifyOperationException("新增通知失败");
        }
    }

    @Override
    public void cancelNotify(String notifyId) throws NotifyOperationException {
        Integer state = notifyMapper.deleteNotify(notifyId);
        if (state <= 0) {
            throw new NotifyOperationException("删除通知失败");
        }
    }

    @Override
    public List<Notify> getAllNotifyList(String userId) throws NotifyOperationException {
        List<Notify> notifyList = notifyMapper.queryAllNotifyList(userId);
        if (notifyList == null || notifyList.isEmpty()) {
            throw new NotifyOperationException("没有消息");
        }
        return notifyList;
    }

    @Override
    public Integer getAllNotifyListCount(String userId) {
        return notifyMapper.queryAllNotifyListCount(userId);
    }

    @Override
    public List<Notify> getNotifyList(String userId, String notifyModuleId) throws NotifyOperationException {
        List<Notify> notifyList = notifyMapper.queryNotifyList(userId, notifyModuleId);
        if (notifyList == null || notifyList.isEmpty()) {
            throw new NotifyOperationException("没有消息");
        }
        return notifyList;
    }

    @Override
    public Notify getNotifyByNotifyTypeAndSenderIdAndTopicId(String notifyModuleId, String senderId, String topicId) {
        return notifyMapper.queryNotifyByNotifyTypeAndSenderIdAndTopicId(notifyModuleId, senderId, topicId);
    }

    @Override
    public void modifyNotifyIsReadByUserIdAndModuleId(String userId, String notifyModuleId) throws NotifyOperationException {
        Integer state = notifyMapper.updateNotifyIsReadByUserIdAndModuleId(userId, notifyModuleId);
//        if (state <= 0) {
//            throw new NotifyOperationException("更新已读状态失败");
//        }
    }

    // ================================================

    @Override
    public List<Notify> getAnnounceList(String userId) {
        return notifyMapper.queryAnnounceList(userId);
    }

    @Override
    public Integer getAnnounceListCount(String userId) {
        return notifyMapper.queryAnnounceListCount(userId);
    }

    @Override
    public List<Notify> getMessageList(String userId) {
        return notifyMapper.queryMessageList(userId);
    }

    @Override
    public Integer getMessageListCount(String userId) {
        return notifyMapper.queryMessageListCount(userId);
    }

    @Override
    public List<Notify> getGreatList(String userId) {
        return notifyMapper.queryGreatList(userId);
    }

    @Override
    public Integer getGreatListCount(String userId) {
        return notifyMapper.queryGreatListCount(userId);
    }

    @Override
    public List<Notify> getCommentList(String userId) {
        return notifyMapper.queryCommentList(userId);
    }

    @Override
    public Integer getCommentListCount(String userId) {
        return notifyMapper.queryCommentListCount(userId);
    }




}
